﻿using System;
using IOSendInterface;
using PipeServer.Manage;
using PipeServer.Model;
using PipeServer.Server;

namespace PipeServer
{
        public class PipeSendClient 
        {
                private readonly string _PipeName = string.Empty;
                private readonly int _TimeOut = 0;
                private readonly int _SyncTimeOut = 0;
                public PipeSendClient (string pipeName)
                {
                        this._PipeName = pipeName;
                        this._TimeOut = Config.PipeConfig.TimeOut;
                        this._SyncTimeOut = Config.PipeConfig.SyncTimeOut;
                }
                public PipeSendClient (string pipeName, int timeOut)
                {
                        this._PipeName = pipeName;
                        this._TimeOut = timeOut;
                        this._SyncTimeOut = timeOut * 1200;
                }
                public PipeSendClient ()
                {
                        this._PipeName = PipeServer.DefaultPipeName;
                        this._TimeOut = Config.PipeConfig.TimeOut;
                        this._SyncTimeOut = Config.PipeConfig.SyncTimeOut;
                }

                public bool CheckState (Guid clientId)
                {
                        return PipeServer.GetServer (this._PipeName, out ServerInfo server) && server.CheckIsCon (clientId);
                }

                public void BatchSend<T> (string type, T data, Guid[] clientId)
                {
                        PipeServer.BatchSend (this._PipeName, type, data, clientId, this._TimeOut, this._SyncTimeOut);
                }

                public bool Send<T> (string type, Guid clientId, T data, out string error) where T : class
                {
                        return PageManage.Send (Page.GetSingleDataPage (this._PipeName, clientId, type, data, this._TimeOut, this._SyncTimeOut), out error);
                }
                public void Send<T> (string type, Guid clientId, T data, Async async, object arg) where T : class
                {
                        PageManage.Send (Page.GetSingleDataPage (this._PipeName, clientId, type, data, this._TimeOut, this._SyncTimeOut), async, arg);
                }
                public void Send (string type, Guid clientId, Async async, object arg)
                {
                        PageManage.Send (Page.GetSingleDataPage (this._PipeName, clientId, type, this._TimeOut, this._SyncTimeOut), async, arg);
                }
                public bool Send<T, Result> (string type, Guid clientId, T data, out Result result, out string error) where T : class where Result : class
                {
                        return PageManage.Send (Page.GetDataPage (this._PipeName, clientId, type, data, this._TimeOut, this._SyncTimeOut), out result, out error);
                }
                public bool Send<Result> (string type, Guid clientId, out Result result, out string error) where Result : class
                {
                        return PageManage.Send (Page.GetDataPage (this._PipeName, clientId, type, this._TimeOut, this._SyncTimeOut), out result, out error);
                }
        }
}
